﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ARR.UI.Models;
using System.Windows.Input;
using ARR.Entities;
using Telerik.Windows.Controls;
using System.Collections.ObjectModel;
using ARR.UI.Commands;
using ARR.Shared;

namespace ARR.UI.ViewModels
{
    public class CategoryMasterViewModel : WorkspaceViewModel
    {
        private ICategoryMasterModel model;
        private ICommand selectionChangedCommand;
        private ICommand saveCommand;
        private ICommand cancelCommand;
        private ICommand deleteCommand;



        public CategoryMasterViewModel(ICategoryMasterModel model)
        {
            this.DisplayName = Labels.MaestroDeCategorias;
            this.model = model;
        }



        public ICommand SelectionChangedCommand
        {
            get
            {
                if (selectionChangedCommand == null)
                {
                    selectionChangedCommand = new RelayCommand(e => GetSelectedItems(e as SelectionChangeEventArgs));
                }
                return selectionChangedCommand;
            }
        }

        public ICommand SaveCommand
        {
            get
            {
                if (saveCommand == null)
                {
                    saveCommand = new RelayCommand(e => Save());
                }
                return saveCommand;
            }
        }

        public ICommand CancelCommand
        {
            get
            {
                if (cancelCommand == null)
                {
                    cancelCommand = new RelayCommand(e => Cancel());
                }
                return cancelCommand;
            }
        }

        public ICommand DeleteCommand
        {
            get
            {
                if (deleteCommand == null)
                {
                    deleteCommand = new RelayCommand(e => Delete());
                }
                return deleteCommand;
            }
        }

        public ObservableCollection<Category> Categories
        {
            get
            {
                return model.Categories;
            }
        }

        public ObservableCollection<CategoryUniverse> Universes
        {
            get
            {
                return model.Universes;
            }
        }

        public ObservableCollection<CategoryRole> Roles
        {
            get
            {
                return model.Roles;
            }
        }

        public List<Category> SelectedCategories { get; set; }



        private void GetSelectedItems(SelectionChangeEventArgs e)
        {
            try
            {
                List<Category> added = e.AddedItems.Cast<Category>().ToList();
                List<Category> removed = e.RemovedItems.Cast<Category>().ToList();

                SelectedCategories.AddRange(added);
                removed.ForEach(at => SelectedCategories.Remove(at));
            }
            catch
            {
            }
        }

        private void Save()
        {
            if (AreValid())
            {
                model.Save();
                model.LoadCategories();

                OnPropertyChanged<CategoryMasterViewModel>(p => p.Categories);
            }
        }

        private void Cancel()
        {
            model.LoadCategories();

            OnPropertyChanged<CategoryMasterViewModel>(p => p.Categories);
        }

        private void Delete()
        {

        }

        private bool AreValid()
        {
            foreach (Category cat in Categories)
            {
                if (!cat.IsValid)
                    return false;
            }

            return true;
        }
    }
}
